package com.zou.music_downloader;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.zou.music_downloader.adapter.SongListAdapter;
import com.zou.music_downloader.bean.ArtistBean;
import com.zou.music_downloader.bean.SongListEntity;
import com.zou.music_downloader.utils.BMA;
import com.zou.music_downloader.utils.VolleyUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 专辑页面
 * Created by zou on 2016/3/13.
 */
@EActivity(R.layout.activity_actist)
public class ArtistActivity extends AppCompatActivity {
    @ViewById(R.id.rcv_artist)
    RecyclerView rcv;

    @ViewById(R.id.errorview_artist)
    LinearLayout errorview_recommend;

    private List<SongListEntity> datas;
    private SongListAdapter      adapter;
    Gson gson = new Gson();

    @AfterViews
    void afterViews() {

        datas = new ArrayList<>();
        adapter = new SongListAdapter(this, datas, getSupportFragmentManager());
        rcv.setLayoutManager(new LinearLayoutManager(this));
        rcv.setAdapter(adapter);



        requestDatas();
    }

    @UiThread
    void requestDatas() {
        RequestQueue queue = VolleyUtil.getQueue(this);
        StringRequest request = new StringRequest(BMA.Artist.artistSongList(null,getIntent().getStringExtra("id"),0,0)
                , new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                handleJsonString(response);
            }
        }
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        queue.add(request);
    }

    private void handleJsonString(String response) {
        datas.clear();
        datas.addAll(gson.fromJson(response, ArtistBean.class).getSonglist());
        adapter.notifyDataSetChanged();
    }
}
